package com.company.indicationshots.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.company.indicationshots.model.Score;

import java.util.ArrayList;
import java.util.List;
//主页面，当前射靶坐标系绘制
public class CurrCoordinateSystemView extends View {
    private Paint paint;
    private int bulletCount;

    private List<Score> coordList = new ArrayList<>();

    public CurrCoordinateSystemView(Context context) {
        super(context);
        init();
    }

    public CurrCoordinateSystemView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CurrCoordinateSystemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void setBulletCount(int bulletCount) {
        this.bulletCount = bulletCount;
        invalidate();
    }

    public void setData(List<Score> coordList) {
        this.coordList = coordList;
        init();
        invalidate();
    }

    private void init() {
        paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(1);
        paint.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();
        int originX = 40; // 原点的X坐标
        int originY = height - 30; // 原点的Y坐标
        int gridSpacingX = 55; // X轴网格间距
        int gridSpacingY = 21; //Y轴网格间距
        int labelOffset = 20; // 标签与刻度线的垂直间距
        int textSize = 17; // 刻度标签的文本大小


        // 绘制X轴的第一象限部分（只绘制正半轴）
        canvas.drawLine(originX, originY, width - 5, originY, paint);

        // 绘制Y轴的第一象限部分（只绘制正半轴）
        canvas.drawLine(originX, originY, originX, 5, paint);

        // 绘制X轴刻度（只绘制第一象限的刻度）
        for (int x = originX + gridSpacingX; x < width; x += gridSpacingX) {
            canvas.drawLine(x, originY, x, originY + 5, paint);
        }

        // 绘制Y轴刻度（只绘制第一象限的刻度）
        for (int y = originY - gridSpacingY; y > gridSpacingY; y -= gridSpacingY) {
            canvas.drawLine(originX - 5, y, originX, y, paint);
        }

        paint.setTextSize(textSize);
        paint.setTextAlign(Paint.Align.CENTER);

        double roundedUp = Math.ceil((double) bulletCount / 10);
        int diff = (int) (roundedUp * 10) / 10;

        // 绘制X轴标签（x轴根据子弹数量变化）
        for (int x = 0; x <= 10; x++) {
            int actualX = originX + x * gridSpacingX; // 将逻辑坐标转换为实际坐标
            if (actualX >= 0 && actualX <= width) {
                // 绘制刻度标签
                String label = String.valueOf(x * diff);
                float textWidth = paint.measureText(label);
                canvas.drawText(label, actualX - textWidth / 2 + 5, originY + labelOffset, paint);
            }
        }


        Paint paintLabels = new Paint();
        paintLabels.setColor(Color.WHITE); // 设置标签颜色
        paintLabels.setTextSize(textSize); // 设置标签文本大小
        paintLabels.setStyle(Paint.Style.STROKE);
        paintLabels.setStrokeWidth(1);
        paintLabels.setAntiAlias(true);

        // 绘制Y轴标签（从5到10）
        for (int y = 5; y <= 10; y++) {
            int actualY = originY - (y - 5) * gridSpacingY; // 计算Y轴上的刻度位置
            if (actualY >= 0 && actualY <= height) {
                // 绘制刻度标签
                String label = String.valueOf(y);
                float textWidth = paintLabels.measureText(label);
                canvas.drawText(label, originX - textWidth - labelOffset + 12, actualY - 16, paintLabels);
            }
        }

        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setAntiAlias(true);

        Score pre = null;
        Score curr = null;
        float preX = 0;
        float preY = 0;
        float currX = 0;
        float currY = 0;
        for (int i = 0; i < coordList.size(); i++) {
            if (i > 0) {
                pre = coordList.get(i - 1);
            }
            curr = coordList.get(i);

            if (i > 0) {
                preX = originX + pre.getNum() * ((float) gridSpacingX / diff);
                preY = originY - ((pre.getRing() - 4.0f) * gridSpacingY);
                if (pre.getRing() < 5.0f) {
                    preY = originY - ((3.5f - 4.0f) * gridSpacingY);
                }
            }
            currX = originX + curr.getNum() * ((float) gridSpacingX / diff);
            currY = originY - ((curr.getRing() - 4.0f) * gridSpacingY);

            if (curr.getRing() < 5.0f) {
                currY = originY - ((3.5f - 4.0f) * gridSpacingY);
                paint.setTextSize(12);
                canvas.drawText("•", currX, currY + 3, paint);
            } else {
                paint.setTextSize(17);
                canvas.drawText("+", currX, currY + 6, paint);
            }

            if (i > 0) {
                canvas.drawLine(preX, preY, currX, currY, paint);
            }
        }

    }
}
